package com.ks.community.controller;


import com.atguigu.commonutils.R;
import com.ks.community.entity.Post;
import com.ks.community.entity.vo.PostVo;
import com.ks.community.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dCoder
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/community/post")
public class PostController {
    @Autowired
    PostService postService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping ("/add")
    public R addPost(@RequestBody PostVo postVo){
        String userId = stringRedisTemplate.opsForValue().get("userId");
        if (userId == null){
            return R.error().message("请先登录");
        }
        postVo.setUploaderId(userId);
        postService.addOne(postVo);
        return R.ok();

    }
    @GetMapping("/Select")
    public R disPlay(){
        String upId = stringRedisTemplate.opsForValue().get("userId");
        List<Post> list = postService.SelectById(upId);
        list.forEach(item->{
            System.out.println(item);
        });
        return  R.ok().data("posts",list);

    }
    @PostMapping("/findAll")
    public R findAll(){
        List<Post> posts = postService.SelectAll();
        return R.ok().data("postAll",posts);
    }

}